import java.util.Arrays;
import java.util.Scanner;

/**
 * @author: gao
 * @create-date: 2025/3/21 21:01
 */

public class Solution {
    public void duplicateZeros(int[] arr) {
        int cur = 0, dest = -1, n = arr.length;

        //1. 先确定最后一个要复写的数
        while (cur < n) {
            if (arr[cur] != 0)
                dest++;
            else
                dest += 2;
            if (dest >= n - 1) {
                break;
            }
            cur++;
        }

        //2. 确定一下边界情况
        if (dest == n) {
            arr[n - 1] = 0;
            cur--;
            dest -= 2;
        }

        //3. 从后向前完成复写操作
        while (cur >= 0) {
            if (arr[cur] == 0) {
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            } else {
                arr[dest--] = arr[cur--];
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int len = in.nextInt();
            int[] arr = new int[len];
            for (int i = 0; i < len; i++) {
                arr[i] = in.nextInt();
            }
            Solution solution = new Solution();
            solution.duplicateZeros(arr);
            System.out.println(Arrays.toString(arr));
        }
    }
}